from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Menu(models.Model):
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
    name = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.name

class Node(models.Model):
    menu = models.OneToOneField(Menu)
    
    def __unicode__(self):
        return self.get_absolute_url()
    
    class Meta:
        abstract = True

class ModelNode(Node):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    
    def get_absolute_url(self):
        try:
            return self.content_object.get_absolute_url()
        except AttributeError:
            return 'No get_absolute_url method on this model.'

class URLNode(Node):
    url = models.CharField(max_length=200)
    
    def get_absolute_url(self):
        return self.url